#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QListWidgetItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPushButton>
#include <QCheckBox>
#include <QSlider>
#include <QLabel>
#include <QGraphicsPixmapItem>
#include <QResizeEvent>

#include "clothes_data.h"
#include "dragable_item.h"
#include "resource_manager.h"
#include "audio_manager.h"
#include "scale_manager.h"
#include "theme_manager.h"
#include "combo_checker.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public slots:
    void checkClothesCollision(DragablePixmapItem* clothesItem);
    void wearClothes(DragablePixmapItem* clothesItem);
    ClothesInfo getClothesInfoByItem(DragablePixmapItem* item);
    void updateBackground();
private:
    int m_currentScene; // 0:未设置, 1:房间, 2:教室
    bool m_showingCombo; // 是否正在显示合成图
    QString m_currentComboPath; // 当前合成图路径
    QWidget* rightWidget; // 右侧功能区
    // 中间展示区
    QGraphicsView *m_view;
    QGraphicsScene *m_scene;

    // 左侧标签页列表
    QListWidget *m_upcloList;
    QListWidget *m_downcloList;
    QListWidget *m_shoesList;
    QListWidget *m_dressList;

    // 右侧功能按钮
    QPushButton *m_scene1Btn;
    QPushButton *m_scene2Btn;
    QPushButton *m_saveBtn;
    QPushButton *m_resetBtn;
    QPushButton *m_randomBtn;
    QPushButton *m_confirmBtn;
    QCheckBox *m_themeCheckBox;

    // BGM控制
    QPushButton *m_bgmToggleBtn;
    QSlider *m_volumeSlider;
    QLabel *m_volumeLabel;
    AudioManager *m_audioManager;

    ThemeManager* m_themeManager;
    ScaleManager* m_scaleManager;
    ComboChecker* m_comboChecker;

    // 服饰数据与场景项
    QList<ClothesInfo> m_clothesList;
    QGraphicsPixmapItem *m_sceneItem; // 背景
    DragablePixmapItem *m_upcloItem;  // 上衣
    DragablePixmapItem *m_downcloItem;// 下装
    DragablePixmapItem *m_dressItem;  // 连衣裙
    DragablePixmapItem *m_shoesItem;  // 鞋子

    // 人物相关
    QGraphicsPixmapItem *m_characterItem;
    QString m_characterPath;

    // 套装相关
    QList<OutfitCombo> m_outfitCombos;
    QGraphicsPixmapItem *m_comboItem;
    QLabel *m_loadingLabel;

    // 缩放参数
    const qreal m_clothesScale = 0.22;
    const qreal m_characterScale = 0.35;

    // 私有函数
    void loadCharacter();
    void loadClothes();
    void initUI();
    void initConnections();
    void clearCurrentClothes();
    void addClothesToScene(const ClothesInfo &info);
    ClothesInfo getClothesInfoByName(const QString &name);

    void adjustCharacterAndClothesScale();
    void centerCharacter();
    void updateClothesScale(qreal scaleFactor);
    void updateComboScale(qreal scaleFactor);
    void updateClothesPosition();

    void testSound();
    void checkAudioDevices();
    void initBGM();
    void removeItem(DragablePixmapItem* item); // 移除并删除衣物项

    void loadOutfitCombos();
    bool checkAndShowCombo(); // 移除 const 修饰符，因为函数会修改成员变量
    bool showComboImage(const QString &imagePath);
    void hideCombo();

    void adjustCharacterScale(qreal delta);

protected:
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void onUpListItemClicked(QListWidgetItem* item);
    void onDownListItemClicked(QListWidgetItem* item);
    void onShoesListItemClicked(QListWidgetItem* item);
    void onDressListItemClicked(QListWidgetItem *item);

    void onScene1BtnClicked();
    void onScene2BtnClicked();
    void onSaveBtnClicked();
    void onResetBtnClicked();
    void onRandomBtnClicked();
    void onConfirmBtnClicked();

    void onThemeCheckBoxToggled(bool checked);
    void onBgmToggleBtnClicked();
    void onVolumeChanged(int volume);

};
#endif // MAINWINDOW_H
